
/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.property;

import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.cef.entity.accessor.base.AccessorComparer;
import com.inspur.edp.cef.gencommon.CefPropertyInfo;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.common.MediatePropertyInfo;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.AccessorClassGenContext;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.EntityGeneratorContext;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.property.BaseTypePropGenerator;
import com.inspur.edp.cef.generator.repository.RepositoryGenUtil;
import java.util.ArrayList;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.IfStatement;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.PrefixExpression;
import org.eclipse.jdt.core.dom.PrefixExpression.Operator;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.SuperMethodInvocation;

public class AccessorExplicitPropertyGenerator extends BaseTypePropGenerator {

	private AccessorClassGenContext parentContext;
	private String propName;
	private TypeInfo returnType;
	private MediateType expilcityType;
	private String InnerData = "InnerData";
	protected String valueVarName = "value";

	//J版接口实现均为public
	private java.util.ArrayList<Modifier.ModifierKeyword> modifiers;

	public AccessorExplicitPropertyGenerator(EntityGeneratorContext parContext,String nodeCode,MediatePropertyInfo propertyInfo) {
		this(parContext,nodeCode,propertyInfo, false);
	}

	public AccessorExplicitPropertyGenerator(EntityGeneratorContext parContext,String nodeCode, MediatePropertyInfo propertyInfo, boolean convertToUpperCase) {
		super(propertyInfo, null, convertToUpperCase);
		this.parentContext = parentContext;
		propName = propertyInfo.getPropertyName();
		returnType = propertyInfo.getPropertyType();//.getReturnType();
		this.modifiers = RepositoryGenUtil.createModifierKeywordArray(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
	}

	//J版无隐式实现
//	public static AccessorExplicitPropertyGenerator createExplicit(CefPropertyInfo propertyInfo) {
//		Class type = propertyInfo.getPropertyMethod().getReturnType();
//		if (type == null || !type.isInterface()) {
//			throw new IllegalArgumentException("propertyInfo");
//		}
//		return new AccessorExplicitPropertyGenerator(propertyInfo);
//	}

	public static AccessorExplicitPropertyGenerator createImplicit(EntityGeneratorContext parContext,String nodeCode,MediatePropertyInfo propertyInfo) {
//		if (propertyInfo.getPropertyMethod().getDeclaringClass() == null || !propertyInfo.getPropertyMethod().getDeclaringClass().isInterface()) {
//			throw new IllegalArgumentException("pi");
//		}
		return new AccessorExplicitPropertyGenerator( parContext,nodeCode,propertyInfo);
	}

	public static AccessorExplicitPropertyGenerator createImplicit(EntityGeneratorContext parContext,String nodeCode,MediatePropertyInfo propertyInfo, boolean convertToUpperCase) {
//		if (propertyInfo.getPropertyMethod().getDeclaringClass() == null || !propertyInfo.getPropertyMethod().getDeclaringClass().isInterface()) {
//			throw new IllegalArgumentException("pi");
//		}
		return new AccessorExplicitPropertyGenerator(parContext,nodeCode,propertyInfo, convertToUpperCase);
	}

	@Override
	protected ArrayList<Modifier.ModifierKeyword> getGetterAccessModifier() {
		return (modifiers != null) ? modifiers : new ArrayList<>();
	}
//
//	@Override
//	protected java.lang.Class getExplicitInterfaceType() =>expilcityType;

	/*	get
	{
		return this.InnerData.get_sUdt02Asso();
	}
	}*/
	@Override
	protected Block buildGetMethodBody() {
		ReturnStatement returnStatement = ast.newReturnStatement();
		String methodName = privatePropertyInfo.getGetterMethodName();
		returnStatement.setExpression(RepositoryGenUtil.createMethodInvocation(ast, RepositoryGenUtil.createMethodInvocation(ast, null,"getInnerData", null), methodName, null));
		Block block = ast.newBlock();
		block.statements().add(returnStatement);
		return block;
	}

	/*	set
		{
			base.RaisePropertyChanging("sUdt02Asso", value, this.InnerData.get_sUdt02Asso());
			bool flag = value == this.InnerData.get_sUdt02Asso();
			if (!flag)
			{
				base.TryCopy();
				this.InnerData.set_sUdt02Asso(value);
			}
		}*/
	@Override
	protected Block buildSetMethodBody() {
		Block block = ast.newBlock();
		addBeforeExtendStatements(block);
		String methodName = "raisePropertyChanging";
		String tryCopyMethodName = "tryCopy";
		String setMethodName = privatePropertyInfo.getSetterMethodName();

		SuperMethodInvocation methodInvocation = ast.newSuperMethodInvocation();
		ArrayList arguments = new ArrayList();
		arguments.add(RepositoryGenUtil.createStringLiteral(ast, propName));
		arguments.add(RepositoryGenUtil.createVariableLiteral(ast, valueVarName));
		arguments.add(RepositoryGenUtil.createMethodInvocation(ast, RepositoryGenUtil.createMethodInvocation(ast, null,"getInnerData", null), privatePropertyInfo.getGetterMethodName(), null));
		methodInvocation.arguments().addAll(arguments);
		methodInvocation.setName(ast.newSimpleName(methodName));


		block.statements().add(ast.newExpressionStatement(methodInvocation));

		IfStatement ifStatement = buildEqualsStatement();
		Block thenStatment = ast.newBlock();
		SuperMethodInvocation methodInvocation1 = ast.newSuperMethodInvocation();
		methodInvocation1.setName(ast.newSimpleName(tryCopyMethodName));
		thenStatment.statements().add(ast.newExpressionStatement(methodInvocation1));

		ArrayList setMethodArguments = new ArrayList();
		setMethodArguments.add(RepositoryGenUtil.createVariableLiteral(ast, valueVarName));
		MethodInvocation methodInvocation2 = RepositoryGenUtil.createMethodInvocation(ast, RepositoryGenUtil.createMethodInvocation(ast,null, "getInnerData", null), setMethodName, setMethodArguments);
		thenStatment.statements().add(ast.newExpressionStatement(methodInvocation2));
		ifStatement.setThenStatement(thenStatment);
		block.statements().add(ifStatement);
		return block;
	}

	protected void addBeforeExtendStatements(Block block) {
	}

	protected final IfStatement buildEqualsStatement() {

		if (getPropertyInfo().getIsChildEntity()) {
			return buildEqualsStatement_Array();
		}
		return buildEqualsStatement_Normal();
	}

	/**
	 * if(value.equals(InnerData.getPropertyName()))
	 *
	 * @return
	 */
	protected final IfStatement buildEqualsStatement_Normal() {
		IfStatement ifStatement = ast.newIfStatement();
		ArrayList arguments = new ArrayList();
		arguments.add(RepositoryGenUtil.createMethodInvocation(ast, RepositoryGenUtil.createMethodInvocation(ast, null,"getInnerData", null), privatePropertyInfo.getGetterMethodName(), null));
		arguments.add(RepositoryGenUtil.createVariableLiteral(ast, valueVarName));
		MethodInvocation methodInvocation = RepositoryGenUtil.createMethodInvocation(ast, ast.newName( AccessorComparer.class.getTypeName()), "equals", arguments);
		PrefixExpression prefixExpression = ast.newPrefixExpression();
		prefixExpression.setOperator(Operator.NOT);
		prefixExpression.setOperand(methodInvocation);
		ifStatement.setExpression(prefixExpression);
		return ifStatement;
	}

	/**
	 * if(ArrayUtil.isArrayEquals(array1,array2))
	 *
	 * @return
	 */
	protected final IfStatement buildEqualsStatement_Array() {
		IfStatement ifStatement = ast.newIfStatement();
		ArrayList arguments = new ArrayList();
		arguments.add(RepositoryGenUtil.createVariableLiteral(ast, valueVarName));
		arguments.add(RepositoryGenUtil.createMethodInvocation(ast, RepositoryGenUtil.createMethodInvocation(ast, null,"getInnerData", null), privatePropertyInfo.getGetterMethodName(), null));
		Expression methodExpression = ast.newName(java.util.Arrays.class.getTypeName());
		String methodName = "equals";
		MethodInvocation methodInvocation = RepositoryGenUtil.createMethodInvocation(ast, methodExpression, methodName, arguments);
		PrefixExpression prefixExpression = ast.newPrefixExpression();
		prefixExpression.setOperator(Operator.NOT);
		prefixExpression.setOperand(methodInvocation);
		ifStatement.setExpression(prefixExpression);
		return ifStatement;
	}
}
